import org.omg.CORBA.PUBLIC_MEMBER;

import java.util.Arrays;

class posWrongfulException extends RuntimeException{
    public posWrongfulException() {

    }
    public posWrongfulException(String message) {
        super(message);
    }
}
class MyArrayList {
    int[] elem;
    int usedSize;
    public static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }
    public void display() {
        for(int i = 0; i < this.usedSize; i++){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    public void add(int data) {
        if(isFull()) {
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }
    public boolean isFull() {
        if(size() >= this.elem.length) {
            return true;
        }
        return false;
    }
    public int size() {
        return this.usedSize;
    }
    public void add(int pos,int data) throws posWrongfulException{
        if(isFull()) {
            System.out.println("满了");
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("pos位置不合法");
            throw new posWrongfulException("pos位置不合法");
        }
        for(int i = this.usedSize - 1; i <= pos; i--) {
            this.elem[i+1]=this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
}
public class Test {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
        myArrayList.add(6);
        myArrayList.add(3);
        myArrayList.display();
        try {
            myArrayList.add(1,9);
            //myArrayList.add(11,3);
        }catch (posWrongfulException e) {
            e.printStackTrace();
        }
        myArrayList.display();
    }
}
